﻿#include "mainaction.h"

MainAction::MainAction(QObject *parent) :
    QThread(parent)
{
 initialization();
}

void MainAction::initialization()
{
    initUdp();
    initDatebase();   
    initTimer();
}

void MainAction::initTimer()
{
    timer_dataBaseLink = new QTimer(this);
    connect(timer_dataBaseLink,SIGNAL(timeout()),this,SLOT(dataBaseLink()));
    timer_dataBaseLink->start(1000);
}


void MainAction::initDatebase()
{
    this->mysql_link = new mySqlLink(datebase::GetInstance());
    mysql_conTime=QDateTime::currentDateTime();
}

void MainAction::initUdp()
{
    udpLink = new udpSocket();
    //建立UDP链接
    udpLink->udpBroadLink = new QUdpSocket(this);
    udpLink->udpBroadLink->bind(systemFile::GetInstance()->udpRecivePort,QUdpSocket::ShareAddress);
    qDebug()<<"RecivePort="<<systemFile::GetInstance()->udpRecivePort;
    connect(udpLink->udpBroadLink,SIGNAL(readyRead()),this,SLOT(udpRecProcess()));
    connect(udpLink,SIGNAL(singalSetAxis()),this,SLOT(slotRec()));
}


void MainAction::UdpRecive()
{
    qDebug()<<"**********MainAction::UdpRecive***********";
}

void MainAction::slotRec()
{
    qDebug()<<"get  emit signalaxis";
    emit signalaxis();
}

void MainAction::udpRecProcess()
{   
//    udpLink->udp_broadcast_reciver_data();
//    qDebug()<<"接收udp发送的消息!";
//    if(udpLink->dev_reboot)
//    {
//         char buf[30]="reboot";
//         ::system(buf);
//        qDebug()<<"udpLink->dev_reboot";

//    }
}


void MainAction::dataBaseLink()
{
    timer_dataBaseLink->stop();
    if(datebase::GetInstance()->mySql_db.isOpen())
    {
        emit sendSql();
        timer_dataBaseLink->start(mysql_connectTime);
        if(mysql_conTime.secsTo(QDateTime::currentDateTime())<60*2)
        {
            return;
        }
    }
    else
    {
        timer_dataBaseLink->start(mysql_disconnectTime);
        qDebug()<<"mySqldb Link...";
    }
    mysql_conTime=QDateTime::currentDateTime();
    this->mysql_link->start();
}
